	<?php
App::uses('AppController', 'Controller');
/**
 * Resources Controller
 *
 * @property Resource $Resource
 */
class ResourcesController extends AppController {

/**
 * index method
 *
 * @return void
 */
	public function index() {
		$this->Resource->recursive = 0;
		$this->set('resources', $this->paginate());
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		if (!$this->Resource->exists($id)) {
			throw new NotFoundException(__('Invalid resource'));
		}
		$options = array('conditions' => array('Resource.' . $this->Resource->primaryKey => $id));
		$this->set('resource', $this->Resource->find('first', $options));
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
	if (CakeSession::read("Auth.User.UserType") != 'Administrator') {
        // No match, redirect the user back to the index action.
        $this->Session->setFlash(__('You cant access this!'));
        $this->redirect('/');
    }
		if ($this->request->is('post')) 
		{
			//upload file
            $fileOK = $this->uploadFiles('files/resources', $this->request->data['Resource']['resource_link']);

							//debug($this->request->data['Resource']['resource_link']);

            if(array_key_exists('urls', $fileOK))
            {
                $this->request->data['Resource']['resource_link'] = $fileOK['urls'][0];
				$this->request->data['Resource']['resource_type'] = $fileOK['type'][0];
				
				
				if ($this->Resource->saveAll($this->request->data)) {
					$this->Session->setFlash(__('The Resource has been saved'));
					$this->redirect(array('action' => 'index'));
				}
            }
			else
			{
				$this->Session->setFlash('Choose a file to upload');
			}
		}
		$categories = $this->Resource->ResourceCategory->find('list');
		$this->set(compact('categories'));
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
	if (CakeSession::read("Auth.User.UserType") != 'Administrator') {
        // No match, redirect the user back to the index action.
        $this->Session->setFlash(__('You cant access this!'));
        $this->redirect('/');
    }
		if (!$this->Resource->exists($id)) {
			throw new NotFoundException(__('Invalid resource'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Resource->save($this->request->data)) {
				$this->Session->setFlash(__('The resource has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The resource could not be saved. Please, try again.'));
			}
		} else {
			$options = array('conditions' => array('Resource.' . $this->Resource->primaryKey => $id));
			$this->request->data = $this->Resource->find('first', $options);
			$categories = $this->Resource->ResourceCategory->find('list');
			$this->set(compact('categories'));
		}
		
	}

/**
 * delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
	if (CakeSession::read("Auth.User.UserType") != 'Administrator') {
        // No match, redirect the user back to the index action.
        $this->Session->setFlash(__('You cant access this!'));
        $this->redirect('/');
    }
		$this->Resource->id = $id;
		$options = array('conditions' => array('Resource.' . $this->Resource->primaryKey => $id));
		$resource = $this->Resource->find('first', $options);
		if (!$this->Resource->exists()) {
			throw new NotFoundException(__('Invalid resource'));
		}
		
		$this->request->onlyAllow('post', 'delete');
		
			//fetches the resource information to get link to file
			
			
			
			$file = new File($resource['Resource']['resource_link']);
			//PROBLEM FILE NOT DELETING FROM WEBROOT
			if($file->delete())
			{
				echo "File deleted successfully";
				if ($this->Resource->delete()) 
				{
					$this->Session->setFlash(__('Resource deleted sucessfully'));
					$this->redirect(array('action' => 'index'));
				}
				else
				{
				$this->Session->setFlash(__('Resource was not deleted'));
				$this->redirect(array('action' => 'index'));
				}
			}
			else
			{
				$this->Session->setFlash(__('File failed to be delete'));
				$this->redirect(array('action' => 'index'));
			}

		/*}
		
		*/
	}
}
